package com.baidu.navi.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.lbsapi.auth.LBSAuthManagerListener;
import com.baidu.navisdk.BNaviEngineManager.NaviEngineInitListener;
import com.baidu.navisdk.BNaviPoint;
import com.baidu.navisdk.BaiduNaviManager;
import com.baidu.navisdk.BaiduNaviManager.OnStartNavigationListener;
import com.baidu.navisdk.comapi.routeplan.RoutePlanParams.NE_RoutePlan_Mode;
import com.baidu.navisdk.util.verify.BNKeyVerifyListener;
import com.simu.towercrane.R;

/**
 * GPS导航Demo
 */

public class RouteGuideDemo extends Activity {
	private BNaviPoint mStartPoint = new BNaviPoint(116.30142, 40.05087,
			"百度大厦", BNaviPoint.CoordinateType.GCJ02);
	private BNaviPoint mEndPoint = new BNaviPoint(116.39750, 39.90882, "北京天安门",
			BNaviPoint.CoordinateType.GCJ02);
	private List<BNaviPoint> mViaPoints = new ArrayList<BNaviPoint>();
	private double startlatitude;
	private double startlongitude;
	private double endlatitude;
	private double endlongitude;
	private String startName;
	private String endNamge;

	// private Button mBtnAddViaPoint;
	private final static String ACCESS_KEY = "uxEBMkmYyGHnLBCFndp73Qvv";
	private static final String CATEGORY_SDK_DEMO = "android.intent.category.BAIDUNAVISDK_DEMO";

	// NX4p2vcBz1rq5SWGh6OSA2Ux sdkdemotest.keystore的sha1值
	private boolean mIsEngineInitSuccess = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_routeguide);
		TextView Tvstart_node = (TextView) findViewById(R.id.start_node);
		TextView Tvend_node = (TextView) findViewById(R.id.end_node);
		Intent intent = getIntent();
		startlatitude = Double.valueOf(intent.getStringExtra("startlatitude"))
				.doubleValue();
		startlongitude = Double
				.valueOf(intent.getStringExtra("startlongitude")).doubleValue();
		endlatitude = Double.valueOf(intent.getStringExtra("endlatitude"))
				.doubleValue();
		endlongitude = Double.valueOf(intent.getStringExtra("endlongitude"))
				.doubleValue();

		startName = intent.getStringExtra("startName");
		endNamge = intent.getStringExtra("endNamge");
		Tvstart_node.setText("起点："+startName);
		Tvend_node.setText("终点："+endNamge);
		BaiduNaviManager.getInstance().initEngine(this, getSdcardDir(),
				mNaviEngineInitListener, new LBSAuthManagerListener() {
					@Override
					public void onAuthResult(int status, String msg) {
						String str = null;
						if (0 == status) {
							str = "key校验成功!";
						} else {
							str = "key校验失败, " + msg;
						}
						Toast.makeText(RouteGuideDemo.this, str,
								Toast.LENGTH_LONG).show();
					}
				});

		Button btnStartNavigation = (Button) findViewById(R.id.button_navigation);
		btnStartNavigation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				launchNavigator2(startlatitude, startlongitude, endlatitude,
						endlongitude, startName, endNamge);
			}
		});

		// findViewById(R.id.start_nav2_btn).setOnClickListener(new
		// OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// if (mViaPoints.size() == 0) {
		// launchNavigator2();
		// } else {
		// launchNavigatorViaPoints();
		// }
		// }
		// });

		// mBtnAddViaPoint = (Button) findViewById(R.id.add_via_btn);
		// mBtnAddViaPoint.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// addViaPoint();
		// }
		// });
	}

	private String getSdcardDir() {
		if (Environment.getExternalStorageState().equalsIgnoreCase(
				Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory().toString();
		}
		return null;
	}

	private NaviEngineInitListener mNaviEngineInitListener = new NaviEngineInitListener() {
		public void engineInitSuccess() {
			mIsEngineInitSuccess = true;
		}

		public void engineInitStart() {
		}

		public void engineInitFail() {
		}
	};

	private BNKeyVerifyListener mKeyVerifyListener = new BNKeyVerifyListener() {

		@Override
		public void onVerifySucc() {
			// TODO Auto-generated method stub
			Toast.makeText(RouteGuideDemo.this, "key校验成功", Toast.LENGTH_LONG)
					.show();
		}

		@Override
		public void onVerifyFailed(int arg0, String arg1) {
			// TODO Auto-generated method stub
			Toast.makeText(RouteGuideDemo.this, "key校验失败", Toast.LENGTH_LONG)
					.show();
		}
	};

	/**
	 * 启动GPS导航. 前置条件：导航引擎初始化成功
	 */
	private void launchNavigator() {
		// 这里给出一个起终点示例，实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标
		BaiduNaviManager.getInstance().launchNavigator(this, 40.05087,
				116.30142, "百度大厦", 39.90882, 116.39750, "北京天安门",
				NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, // 算路方式
				true, // 真实导航
				BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, // 在离线策略
				new OnStartNavigationListener() { // 跳转监听

					@Override
					public void onJumpToNavigator(Bundle configParams) {
						Intent intent = new Intent(RouteGuideDemo.this,
								BNavigatorActivity.class);
						intent.putExtras(configParams);
						startActivity(intent);
					}

					@Override
					public void onJumpToDownloader() {
					}
				});
	}

	/**
	 * 指定导航起终点启动GPS导航.起终点可为多种类型坐标系的地理坐标。 前置条件：导航引擎初始化成功
	 * 
	 * @param endNamge
	 * @param startName
	 * @param endlongitude
	 * @param endlatitude
	 * @param startlongitude
	 * @param startlatitude
	 */
	private void launchNavigator2(double startlatitude, double startlongitude,
			double endlatitude, double endlongitude, String startName,
			String endNamge) {
		// 这里给出一个起终点示例，实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标
		BNaviPoint startPoint = new BNaviPoint( startlongitude,startlatitude,
				startName, BNaviPoint.CoordinateType.BD09_MC);
		BNaviPoint endPoint = new BNaviPoint(endlongitude,endlatitude, 
				endNamge, BNaviPoint.CoordinateType.BD09_MC);
		BaiduNaviManager.getInstance().launchNavigator(this, startPoint, // 起点（可指定坐标系）
				endPoint, // 终点（可指定坐标系）
				NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, // 算路方式
				true, // 真实导航
				BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, // 在离线策略
				new OnStartNavigationListener() { // 跳转监听

					@Override
					public void onJumpToNavigator(Bundle configParams) {
						Intent intent = new Intent(RouteGuideDemo.this,
								BNavigatorActivity.class);
						intent.putExtras(configParams);
						startActivity(intent);
					}

					@Override
					public void onJumpToDownloader() {
					}
				});
	}

	/**
	 * 增加一个或多个途经点，启动GPS导航. 前置条件：导航引擎初始化成功
	 */
	private void launchNavigatorViaPoints() {
		// 这里给出一个起终点示例，实际应用中可以通过POI检索、外部POI来源等方式获取起终点坐标
		// BNaviPoint startPoint = new BNaviPoint(113.97348, 22.53951, "白石洲");
		// BNaviPoint endPoint = new BNaviPoint(113.92576, 22.48876, "蛇口");
		// BNaviPoint viaPoint1 = new BNaviPoint(113.94104, 22.54343, "国人通信大厦");
		// BNaviPoint viaPoint2 = new BNaviPoint(113.94863, 22.53873,
		// "中国银行科技园支行");
		List<BNaviPoint> points = new ArrayList<BNaviPoint>();
		points.add(mStartPoint);
		points.addAll(mViaPoints);
		points.add(mEndPoint);
		BaiduNaviManager.getInstance().launchNavigator(this, points, // 路线点列表
				NE_RoutePlan_Mode.ROUTE_PLAN_MOD_MIN_TIME, // 算路方式
				true, // 真实导航
				BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, // 在离线策略
				new OnStartNavigationListener() { // 跳转监听

					@Override
					public void onJumpToNavigator(Bundle configParams) {
						Intent intent = new Intent(RouteGuideDemo.this,
								BNavigatorActivity.class);
						intent.putExtras(configParams);
						startActivity(intent);
					}

					@Override
					public void onJumpToDownloader() {
					}
				});
	}
}
